#ifndef SINGLETON_TEST_H
#define SINGLETON_TEST_H

#include <pthread.h>
#include <stdlib.h>
#include <cstddef>
#include <iostream>

class SingletonTest {
public:
   static SingletonTest& instance() {
       pthread_once(&ponce, init);

       return *value;
   } 

    static SingletonTest* instance_p() {
        pthread_once(&ponce, init);

        return value;
    }
private:
    static void init() {
        std::cout << "init" << std::endl;
        value = new SingletonTest();
        ::atexit(destory); // 终⽌处理函数,由exit⾃动调⽤
    }

    static void destory() {
        if (value != nullptr) {
            delete value;
            value = nullptr;
        }
    }

    static SingletonTest *value;
    static pthread_once_t ponce;
};

#endif
